package com.sunspring.jbug.controller;

import com.sunspring.jbug.bean.OperationLog;
import com.sunspring.jbug.bean.Project;
import com.sunspring.jbug.bean.UserInfo;
import com.sunspring.jbug.bo.ProjectAndUserMapping;
import com.sunspring.jbug.common.LogEnum;
import com.sunspring.jbug.common.SimpleResponse;
import com.sunspring.jbug.service.OperationLogService;
import com.sunspring.jbug.service.ProjectService;
import com.sunspring.jbug.support.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wulei
 * @date 2019-03-01
 **/
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private OperationLogService operationLogService;

    /**
     * 创建项目
     * @return 项目Id
     */
    @PostMapping("/create")
    public SimpleResponse create(Project project) {
        Integer projectId = projectService.createProject(project);
        //添加操作记录
        UserInfo currentUser = ContextHolder.get();
        OperationLog operationLog = OperationLog.builder()
                .type(LogEnum.PROJECT.getCode())
                .aboutId(projectId)
                .message(currentUser.getName() + "创建了项目：" + project.getProjectName())
                .build();
        operationLogService.addLog(operationLog);
        return SimpleResponse.success(projectId);
    }

    /**
     * 添加项目成员
     * @param memberList ProjectAndUserMapping list
     */
    @PostMapping("/addMember")
    public SimpleResponse addProjectMember(List<ProjectAndUserMapping> memberList) {
        projectService.addProjectMember(memberList);
        return SimpleResponse.success();
    }

    /**
     * 我的项目
     */
    @GetMapping("/myProject")
    public SimpleResponse myProject() {
        return SimpleResponse.success(projectService.getMyProjects());
    }


}
